package com.shenmazong.serverossaliyun.controller;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.PutObjectResult;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.UUID;

/**
 * @author 田不平
 * @project server-security
 * @date 2020/3/10 8:53 AM
 * http://www.shenmazong.com
 */
@Controller
public class UploadController {

    @RequestMapping(value = "/")
    public String index() {
        return "index";
    }

    public String uploadAliyun(MultipartFile file, String fileName) {
        String url = null;
        // Endpoint以杭州为例，其它Region请按实际情况填写。
        String endpoint = "http://oss-cn-beijing.aliyuncs.com";
        // 阿里云主账号AccessKey拥有所有API的访问权限，风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维，请登录 https://ram.console.aliyun.com 创建RAM账号。

        // shenmazong@1642590691341298.onaliyun.com
        String accessKeyId = "LTAI4FtqoX9aXnwt5WRPVtJ9";
        String accessKeySecret = "BjxfqbWKvpusWM328UGzwYT8PQYlm5";

        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

        // 创建PutObjectRequest对象。
        // 上传文件流。
//        String localFile = "/Users/hm/david/pics/meixi01.jpeg";
//        InputStream inputStream = null;
//        try {
//            inputStream = new FileInputStream(localFile);
//        } catch (FileNotFoundException e) {
//            e.printStackTrace();
//        }
        PutObjectResult result = null;
        try {
            result = ossClient.putObject("shenmazong", fileName, file.getInputStream());
            url = "https://shenmazong.oss-cn-beijing.aliyuncs.com/" + fileName;
        } catch (IOException e) {
            e.printStackTrace();
        }

        // https://shenmazong.oss-cn-beijing.aliyuncs.com/001.jpeg
        // https://shenmazong.oss-cn-beijing.aliyuncs.com/cd9a0cde-dc30-4620-81f0-db2e3d5b4bdf.jpeg
        //System.out.println(result.toString());
        //String content = "Hello OSS";
        // <yourObjectName>表示上传文件到OSS时需要指定包含文件后缀在内的完整路径，例如abc/efg/123.jpg。
        //PutObjectRequest putObjectRequest = new PutObjectRequest("<yourBucketName>", "<yourObjectName>", new ByteArrayInputStream(content.getBytes()));

        // 如果需要上传时设置存储类型与访问权限，请参考以下示例代码。
        // ObjectMetadata metadata = new ObjectMetadata();
        // metadata.setHeader(OSSHeaders.OSS_STORAGE_CLASS, StorageClass.Standard.toString());
        // metadata.setObjectAcl(CannedAccessControlList.Private);

        // putObjectRequest.setMetadata(metadata);

        // 上传字符串。
        //ossClient.putObject(putObjectRequest);

        // 关闭OSSClient。
        ossClient.shutdown();
        return url;
    }

    @RequestMapping(value = "/upload")
    public String upload(@RequestParam("uploadFile") MultipartFile file) {
        if(file.isEmpty()){
            System.out.println("文件为空");
            return "index";
        }
        String fileName = file.getOriginalFilename();
        int size = (int) file.getSize();
        System.out.println(fileName + "-->" + size);

        UUID uuid = UUID.randomUUID();
        String suffix = fileName.substring(fileName.lastIndexOf(".")).toLowerCase();
        String newName = uuid.toString() + suffix;

        String url = uploadAliyun(file, newName);
        if(url == null) {
            System.out.println("上传失败了");
            return "index";
        }
        System.out.println("上传成功: " + url);

        return "index";
    }
}
